<?php
/**
 *
 *
 */
class User extends AppModel
{
    public $name       = "User";
    public $primaryKey = "userId";
    public $belongsTo  = array('Privilege' =>
                                array('className' => 'Privilege',
                                      'foreignKey' => 'privilegeId'
                                      )
                               );



    public function authenticateUser(array $data)
    {
        $conditions = array_merge($data, array('User.active' => 1, 'Privilege.active' => 1));
        $token = self::find('first',
                            array('conditions' => $conditions,
                                  'fields' =>
                                   array('Privilege.privilegeId', 'User.userId',
                                        'User.fname', 'User.uname',
                                        'User.email_address', 'User.token',
                                        '`Privilege`.`name`')
                                   )
                            );


        if ($token) {
            /**
             * write user details in session
             */
             $sessData['User']['userId'] = $token['User']['userId'];
             $sessData['User']['uname'] = $token['User']['uname'];
             $sessData['User']['fname'] = $token['User']['fname'];
             $sessData['User']['email'] = $token['User']['email_address'];
             $sessData['User']['privilege'] = $token['Privilege']['privilegeId'];
             $sessData['User']['access'] = $token['Privilege']['name'];
             return $sessData;
        }


        return null;
    }

    public function get_user_info($marker, array $condition, $fields = null)
    {
        return self::find($marker, array('conditions' => $condition, 'fields' => $fields));
    }
}
?>